<?php
// 启动会话
session_start();

// 检查是否已登录
if (!isset($_SESSION['admin_logged_in']) || $_SESSION['admin_logged_in'] !== true) {
    header('Location: index.php');
    exit;
}

// 引入数据库配置
require 'config.php';

$success_message = '';
$error = '';

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $host = $_POST['host'] ?? '';
    $port = $_POST['port'] ?? '';
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    $encryption = $_POST['encryption'] ?? '';
    $from_email = $_POST['from_email'] ?? '';
    $from_name = $_POST['from_name'] ?? '';
    $notification_email = $_POST['notification_email'] ?? '';
    $notification_enabled = isset($_POST['notification_enabled']) ? 1 : 0;

    // 验证表单数据
    if (empty($host) || empty($port) || empty($username) || empty($password) || empty($from_email) || empty($from_name)) {
        $error = '请填写所有必填字段';
    } elseif (!is_numeric($port)) {
        $error = '端口必须是数字';
    } elseif (!filter_var($from_email, FILTER_VALIDATE_EMAIL)) {
        $error = '请输入有效的发件人邮箱';
    } else {
        try {
            // 更新SMTP设置
            $sql = "UPDATE smtp_settings SET 
                    host = :host, 
                    port = :port, 
                    username = :username, 
                    password = :password, 
                    encryption = :encryption, 
                    from_email = :from_email, 
                    from_name = :from_name, 
                    notification_email = :notification_email, 
                    notification_enabled = :notification_enabled 
                    WHERE id = 1";
            $stmt = $pdo->prepare($sql);
            $stmt->bindParam(':host', $host);
            $stmt->bindParam(':port', $port, PDO::PARAM_INT);
            $stmt->bindParam(':username', $username);
            $stmt->bindParam(':password', $password);
            $stmt->bindParam(':encryption', $encryption);
            $stmt->bindParam(':from_email', $from_email);
            $stmt->bindParam(':from_name', $from_name);
            $stmt->bindParam(':notification_email', $notification_email);
            $stmt->bindParam(':notification_enabled', $notification_enabled, PDO::PARAM_INT);
            $stmt->execute();

            $success_message = 'SMTP设置更新成功';
        } catch(PDOException $e) {
            $error = '数据库错误: ' . $e->getMessage();
        }
    }
}

// 获取当前SMTP设置
try {
    $sql = "SELECT * FROM smtp_settings WHERE id = 1";
    $stmt = $pdo->query($sql);
    $smtp_settings = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$smtp_settings) {
        $error = '未找到SMTP设置';
    }
} catch(PDOException $e) {
    $error = '数据库错误: ' . $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>✿ SMTP设置 ✿</title>
    <link rel="icon" href="favicon.ico" type="image/x-icon">
    <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=ZCOOL+KuaiLe&family=Ma+Shan+Zheng&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="styles_anime_admin.css">
    <script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/gsap@3.11.3/dist/gsap.min.js"></script>
    <style>
        .settings-container {
            max-width: 600px;
            margin: 30px auto;
            padding: 25px;
            background-color: var(--surface);
            border-radius: 24px;
            box-shadow: 0 10px 30px rgba(255, 107, 139, 0.2);
            position: relative;
            overflow: hidden;
            border: 3px solid var(--border);
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(10px);
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }
        .header h1 {
            margin: 0;
            position: relative;
        }
        .header h1::before,
        .header h1::after {
            content: '✿';
            color: var(--secondary);
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            font-size: 1.5rem;
        }
        .header h1::before {
            left: -30px;
        }
        .header h1::after {
            right: -30px;
        }
        .back-btn, .test-btn {
            padding: 8px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            margin-left: 10px;
        }
        .back-btn {
            padding: 10px 20px;
            background-color: var(--secondary);
            color: white;
            border: none;
            border-radius: 50px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            margin-bottom: 15px;
            font-size: 1rem;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
            box-shadow: 0 4px 8px rgba(138, 92, 242, 0.3);
        }
        .test-btn {
            padding: 10px 20px;
            background-color: var(--accent);
            color: white;
            border: none;
            border-radius: 50px;
            cursor: pointer;
            font-size: 1rem;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
            box-shadow: 0 4px 8px rgba(255, 183, 77, 0.3);
        }
        .test-btn:hover {
            background-color: #ffcc80;
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(255, 183, 77, 0.4);
        }
        .header-actions {
            display: flex;
            align-items: center;
        }
        .back-btn:hover {
            background-color: var(--secondary-light);
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(138, 92, 242, 0.4);
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: var(--primary);
            font-size: 1.1rem;
        }
        .form-group input {
            width: 100%;
            padding: 12px;
            border: 2px solid var(--border);
            border-radius: 12px;
            box-sizing: border-box;
            background-color: rgba(255, 255, 255, 0.7);
            font-size: 1rem;
            transition: all 0.3s ease;
        }
        .form-group input:focus {
            border-color: var(--primary);
            outline: none;
            box-shadow: 0 0 10px rgba(255, 107, 139, 0.2);
        }
        .form-group select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .btn-save {
            padding: 12px 25px;
            background-color: var(--primary);
            color: white;
            border: none;
            border-radius: 50px;
            cursor: pointer;
            font-size: 1rem;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
            box-shadow: 0 4px 8px rgba(255, 107, 139, 0.3);
        }
        .btn-save:hover {
            background-color: var(--primary-light);
            transform: translateY(-2px);
            box-shadow: 0 6px 12px rgba(255, 107, 139, 0.4);
        }
        .success-message {
            color: var(--success);
            margin-bottom: 15px;
            font-weight: bold;
            text-align: center;
            padding: 10px;
            border-radius: 12px;
            background-color: rgba(149, 225, 125, 0.2);
            border: 2px solid var(--success-border);
        }
        .error-message {
            background-color: #e74c3c;
            color: white;
            padding: 15px;
            border-radius: 4px;
            margin-bottom: 20px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="settings-container">
        <div class="header">
            <div class="anime-decoration"></div>
            <h1>SMTP设置</h1>
            <div class="header-actions">
                <a href="test_email.php" class="test-btn">
                <span>测试邮件</span>
                <span class="heart">♡</span>
            </a>
                <a href="admin_dashboard.php" class="back-btn">
                <span>返回仪表盘</span>
                <span class="heart">♡</span>
            </a>
            </div>
        </div>

        <?php if (!empty($error)): ?>
            <div class="error-message"><?php echo $error; ?></div>
        <?php endif; ?>

        <?php if (!empty($success_message)): ?>
            <div class="success-message"><?php echo $success_message; ?></div>
        <?php endif; ?>

        <?php if ($smtp_settings): ?>
            <form method="POST" action="smtp_settings.php">
                <div class="form-group">
                    <label for="host">SMTP服务器地址</label>
                    <input type="text" id="host" name="host" value="<?php echo htmlspecialchars($smtp_settings['host']); ?>" required>
                </div>
                <div class="form-group">
                    <label for="port">SMTP服务器端口</label>
                    <input type="number" id="port" name="port" value="<?php echo htmlspecialchars($smtp_settings['port']); ?>" required>
                </div>
                <div class="form-group">
                    <label for="username">SMTP用户名</label>
                    <input type="text" id="username" name="username" value="<?php echo htmlspecialchars($smtp_settings['username']); ?>" required>
                </div>
                <div class="form-group">
                    <label for="password">SMTP密码/授权码</label>
                    <input type="password" id="password" name="password" value="<?php echo htmlspecialchars($smtp_settings['password']); ?>" required>
                </div>
                <div class="form-group">
                    <label for="encryption">加密方式</label>
                    <select id="encryption" name="encryption">
                        <option value="" <?php echo $smtp_settings['encryption'] === '' ? 'selected' : ''; ?>>无</option>
                        <option value="ssl" <?php echo $smtp_settings['encryption'] === 'ssl' ? 'selected' : ''; ?>>SSL</option>
                        <option value="tls" <?php echo $smtp_settings['encryption'] === 'tls' ? 'selected' : ''; ?>>TLS</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="from_email">发件人邮箱</label>
                    <input type="email" id="from_email" name="from_email" value="<?php echo htmlspecialchars($smtp_settings['from_email']); ?>" required>
                </div>
                <div class="form-group">
                    <label for="from_name">发件人名称</label>
                    <input type="text" id="from_name" name="from_name" value="<?php echo htmlspecialchars($smtp_settings['from_name']); ?>" required>
                </div>
                <div class="form-group">
                    <label for="notification_email">工单通知邮箱</label>
                    <input type="email" id="notification_email" name="notification_email" value="<?php echo isset($smtp_settings['notification_email']) ? htmlspecialchars($smtp_settings['notification_email']) : ''; ?>">
                </div>
                <div class="form-group">
                    <label for="notification_enabled">启用新工单通知</label>
                    <input type="checkbox" id="notification_enabled" name="notification_enabled"<?php echo (isset($smtp_settings['notification_enabled']) && $smtp_settings['notification_enabled']) ? ' checked' : ''; ?>>
                    <span>开启后，新工单将发送通知到上面的邮箱</span>
                </div>
                <button type="submit" class="btn-save">
                <span>保存设置</span>
                <span class="heart">♡</span>
            </button>
            </form>
        <?php endif; ?>
    </div>
<script src="script_anime_admin.js"></script>
</body>
</html>